package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Goods;
import bean.User;
import common.BaseServlet;
import common.CartShop;
import service.GoodsImgDAO;
import service.ProductDAO;

/**
 * Servlet implementation class CartShopServlet
 */
@WebServlet("/CartShop")
public class CartShopServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static final ProductDAO pdao = new ProductDAO();
	private static final GoodsImgDAO gImgdao = new GoodsImgDAO();
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartShopServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	//查看购物车
	public String basefun(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("进入购物车详情");
		HttpSession session=request.getSession(false);
		CartShop cartShop=(CartShop) session.getAttribute("cartShop");
		if(request.getSession(false)==null||session.getAttribute("cartShop")==null) {
			invalidateSession(session);
		}
		Map<Integer,Integer> cartMap=cartShop.getCarts();
		
		//将购物车的商品信息存入字符串
		//"{userid:1,godosList:[{goodsImg:1.jpg,name:荣耀10,price:2799,count:10,discountPrice:200,postFee:0},]}";
		StringBuffer cartListStr=new StringBuffer();
		
		/*int userid=(session.getAttribute("user")==null)?0:
				((User)session.getAttribute("user")).getUserid();*/
		int userid=cartShop.getUserid();
		System.out.println("userid"+userid);
		
		int ifEmpty=(int) session.getAttribute("ifEmpty");
		System.out.println("ifEmpty"+ifEmpty);
		
		cartListStr.append("{\"userid\":\""+userid+"\",\"ifEmpty\":\"");
		//cartListStr.append("{\"userid\":\""+userid+"\",\"goodsList\":[");
		cartListStr.append(ifEmpty+"\",\"goodsList\":[");
		if(ifEmpty==0) {
			for(Map.Entry<Integer, Integer> en:cartMap.entrySet()) {
				int goodsid=en.getKey();
				int goodsNum=en.getValue();
				Goods goods=pdao.queryByGoodsid(goodsid).get(0);
				String goodsImg=gImgdao.queryGoodsImg(goodsid).get(0).getImg01();
				cartListStr.append("{\"goodsImg\":\""+goodsImg+"\",\"name\":\""+
						goods.getName()+"\",\"goodsid\":\""+goods.getGoodsid()+
						"\",\"goodsNum\":\""+goodsNum+
						"\",\"price\":\""+goods.getPrice()+"\",\"count\":\""+
						goods.getCount()+"\",\"discountPrice\":\""+goods.getDiscountPrice()+
						"\",\"postFee\":\""+goods.getPostFee()+"\"},");
			}
			cartListStr.deleteCharAt(cartListStr.length()-1);
		}
		cartListStr.append("]}");
		System.out.println("cartListStr:"+cartListStr);
		request.setAttribute("cartListStr", cartListStr);
		return "f:jsp/CartShop";
	}
	//添加购物车商品
	public void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int goodsid=Integer.parseInt(request.getParameter("goodsid")); 
		int num=Integer.parseInt(request.getParameter("num")); 
		HttpSession session=request.getSession(false);
		if(request.getSession(false)==null||session.getAttribute("cartShop")==null){
			invalidateSession(session);
		}
		CartShop cartShop=(CartShop) session.getAttribute("cartShop");
		int userid=cartShop.getUserid();
		cartShop.addGoods(userid, goodsid, num);
		session.setAttribute("ifEmpty",cartShop.ifEmpty());
		PrintWriter out=response.getWriter();
		out.write("1");
	}
	//删除购物车商品
	public void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("进入删除购物车商品");
		HttpSession session=request.getSession(false);
		if(request.getSession(false)==null||session.getAttribute("cartShop")==null){
			invalidateSession(session);
		}
		int goodsid=Integer.parseInt(request.getParameter("goodsid")); 
		CartShop cartShop=(CartShop) session.getAttribute("cartShop");
		int userid=cartShop.getUserid();
		cartShop.deleteGoods(userid, goodsid);
		session.setAttribute("ifEmpty",cartShop.ifEmpty());
		PrintWriter out=response.getWriter();
		out.write("1");
	}
	
	//改变购物车商品数量
	public void updateNum(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("进入改变购物车商品数量");
		int goodsid=Integer.parseInt(request.getParameter("goodsid")); 
		int goodsNum=Integer.parseInt(request.getParameter("goodsNum")); 
		HttpSession session=request.getSession(false);
		if(request.getSession(false)==null||session.getAttribute("cartShop")==null){
			invalidateSession(session);
		}
		CartShop cartShop=(CartShop) session.getAttribute("cartShop");
		int userid=cartShop.getUserid();
		System.out.println("userid:"+userid+",goodsid:"+goodsid+",goodsNum:"+goodsNum);
		cartShop.updateNum(userid, goodsid, goodsNum);
		session.setAttribute("ifEmpty",cartShop.ifEmpty());
		PrintWriter out=response.getWriter();
		out.write("1");
	}

	

}
